package main

import (
	"lnzw/engine/args"
	"lnzw/engine/define"
	"lnzw/engine/log"
	"lnzw/engine/pprofile"
	"lnzw/engine/work"
	"math/rand"
	"time"
)

const (
	TEST_QUEUE1 define.QueueType = 1
	TEST_QUEUE2 define.QueueType = 2
)

func main() {
	// //配置路径
	// var configPath string
	// //服务器类型
	// var serverType string
	// flag.StringVar(&configPath, "configPath", "../config/game.yaml", "")
	// flag.StringVar(&serverType, "serverType", base.SERVER_TYPE_GATE, "")
	// flag.Parse()
	// var server base.IServer
	// switch serverType {
	// case base.SERVER_TYPE_GATE:
	// 	server = &gate.GateServer{}
	// }
	// server.Start()
	log.Setup()
	work.Setup()
	go func() {
		for {
			for i := 0; i < 3000; i++ {
				work.Mgr.SubmitWork(&work1{}, rand.Intn(1000), "w1")
			}
			time.Sleep(time.Second * 1)
		}
	}()

	go func() {
		for {
			for i := 0; i < 3000; i++ {
				work.Mgr.SubmitWork(&work2{}, rand.Intn(1000), "w2")
			}
			time.Sleep(time.Second * 1)
		}
	}()
	go func() {
		for {
			work.Mgr.PrintPprof()
			time.Sleep(time.Second * 10)
		}
	}()
	pprofile.Setup()
	defer pprofile.Stop()
	defer func() {
		work.Mgr.PrintPprof()
	}()
	time.Sleep(time.Second * 20000000)
}

type work1 struct {
	value int
	name  string
}

func (w *work1) Init(args *args.Args) {
	w.value = args.ReadNext().(int)
	w.name = args.ReadNext().(string)
}
func (w *work1) Run() {
	//time.Sleep(time.Millisecond * time.Duration(rand.Intn(5)))
	num := rand.Intn(500)

	for i := 0; i < num; i++ {
		x := 0
		x = i
		x += 1
	}
	if num > 300 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(3)))
	} else if num > 400 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(10)))
	} else if num > 450 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(20)))
	} else if num > 490 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(30)))
	} else if num > 495 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(50)))
	} else if num > 498 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(400)))
	}
}
func (w *work1) QueueId() define.QueueType {
	return TEST_QUEUE1
}

type work2 struct {
	value int
	name  string
}

func (w *work2) Init(args *args.Args) {
	w.value = args.ReadNext().(int)
	w.name = args.ReadNext().(string)
}
func (w *work2) Run() {
	//time.Sleep(time.Millisecond * time.Duration(rand.Intn(5)))
	num := rand.Intn(500)

	for i := 0; i < num; i++ {
		x := 0
		x = i
		x += 1
	}
	if num > 300 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(3)))
	} else if num > 400 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(10)))
	} else if num > 450 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(20)))
	} else if num > 490 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(30)))
	} else if num > 495 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
	} else if num > 498 {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(600)))
	}
}
func (w *work2) QueueId() define.QueueType {
	return TEST_QUEUE2
}
